/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

namespace DBLinq.MySqlProviders
{
	using System.Web;
	using System.Linq;
	using System.Data.Linq.SqlClient;

	internal static partial class AspNetApplicationsDTO
	{
		internal static void Delete( string ApplicationName)
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				var apps = from a in context.AspNetApplications
					  where a.ApplicationName == ApplicationName
					  select a;
				foreach ( AspNetApplication app in apps )
				{
					context.AspNetApplications.Remove ( app );
				}
				context.SubmitChanges ();
			}
		}

		internal static void Insert( AspNetApplication newApp )
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				context.AspNetApplications.Add ( newApp );
				context.SubmitChanges ();
			}
		}

		internal static uint GetApplicationId( string appName )
		{
			uint appid = 0;
			string appcache = string.Concat ( "ApplicationId", "_", appName );
			if ( (HttpContext.Current == null) || (HttpContext.Current.Cache[ appcache ] == null ))
			{
				if ( appName.Length > 255 )
					appName = appName.Substring ( 0, 255 );
				using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
				{
                    var app = context.AspNetApplications.Where<AspNetApplication>(a => a.ApplicationName == appName).ToList();
                    if (app.Count > 0)
					    appid = app.First().ApplicationId;
					if (HttpContext.Current != null)
						HttpContext.Current.Cache[ appcache ] = appid;
				}
			}
			else
				appid = ( uint ) HttpContext.Current.Cache[ appcache ];
			return appid;
		}

		internal static bool Exist( string _ApplicationName )
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
                return context.AspNetApplications.Where<AspNetApplication>(p => p.ApplicationName == _ApplicationName).ToList().Count > 0;
			}
		}

		public static uint GetLastId( string _ApplicationName )
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
                var q = context.AspNetApplications.Where<AspNetApplication>(a => a.ApplicationName == _ApplicationName).ToList();
                if (q.Count > 0)
                    return q.Last().ApplicationId;
                else
                    return 0;
			}
		}
	}
}